package online.longzipeng.cinemasys.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import online.longzipeng.cinemasys.common.annotion.NeedPermissions;
import online.longzipeng.cinemasys.common.exception.AppException;
import online.longzipeng.cinemasys.common.result.ResponseEnum;
import online.longzipeng.cinemasys.common.result.Result;
import online.longzipeng.cinemasys.dto.customer.CustomerLoginResponseDTO;
import online.longzipeng.cinemasys.dto.customer.CustomerQueryResponseDTO;
import online.longzipeng.cinemasys.entity.Customer;
import online.longzipeng.cinemasys.service.CustomerService;
import online.longzipeng.cinemasys.service.EmailService;
import online.longzipeng.cinemasys.vo.customer.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  客户控制器
 * </p>
 *
 * @author lzp
 * @since 2021-04-08
 */
@RestController
@RequestMapping("/customer")
@Api(value = "客户相关接口",description = "客户相关接口",tags = "客户相关接口")
public class CustomerController {

    @Autowired
    CustomerService customerService;

    @PostMapping("/login")
    @ApiOperation("客户登录系统")
    public Result login(@RequestBody CustomerLoginVO customerLoginVO, HttpServletRequest httpServletRequest){
        CustomerLoginResponseDTO customerLoginResponseDTO = customerService.login(customerLoginVO,httpServletRequest);
        return Result.success(customerLoginResponseDTO,"欢迎回来");
    }

    @PostMapping("/change-password-by-email-check")
    @ApiOperation("通过邮箱验证重置密码")
    public Result changePasswordByEmailCheck(@RequestBody CustomerChangePassWordByEmailVO customerChangePassWordByEmailVO){
        customerService.changePasswordByEmailCheck(customerChangePassWordByEmailVO);
        return Result.success("重置成功");
    }

    @GetMapping("/get-reset-code-by-email")
    @ApiOperation("向指定邮箱发送验证码")
    public Result getResetCode(@RequestParam(value = "userName",required = true) String userName, @RequestParam(value = "email",required = true)String email){
        customerService.getResetCode(userName,email);
        return Result.success();
    }

    @PostMapping("/get-list-page")
    @ApiOperation("查询所有客户带分页")
    public Result getListPage(@RequestBody CustomerQueryVO queryVO){
        IPage<CustomerQueryResponseDTO> customerQueryResponseDTOIPage = customerService.getListPage(queryVO);
        return Result.success(customerQueryResponseDTOIPage);
    }

    @GetMapping("/get-user-info")
    @NeedPermissions("customer:select")
    @ApiOperation("通过id查询客户")
    public Result getUserInfo(@RequestHeader("X-Token")String token){
        CustomerQueryResponseDTO customerQueryResponseDTO = customerService.getUserInfo(token);
        return Result.success(customerQueryResponseDTO);
    }

    @GetMapping("/get-by-id")
    @NeedPermissions("customer:select")
    @ApiOperation("通过id查询客户")
    public Result getById(@ApiParam(value = "客户id",required = true)Long id){
        return Result.success(customerService.findById(id));
    }

    @PostMapping("/save")
    @NeedPermissions("customer:save")
    @ApiOperation("新增客户")
    public Result save(@RequestBody CustomerSaveVO customerSaveVO){
        Customer customer = new Customer();
        BeanUtils.copyProperties(customerSaveVO,customer);
        customerService.add(customer);
        return Result.success(null,"客户添加成功！！");
    }

    @GetMapping("/delete-one")
    @NeedPermissions("customer:delete")
    @ApiOperation("删除客户")
    public Result deleteOne(@ApiParam(value = "客户id",required = true)Long id){
        try {
            customerService.removeById(id);
        } catch (Exception e) {
            e.printStackTrace();
            throw new AppException(ResponseEnum.FAILED);
        }
        return Result.success(null,"客户删除成功！！");
    }

    @GetMapping("/delete")
    @NeedPermissions("customer:delete")
    @ApiOperation("删除多个客户")
    public Result deleteOne(@ApiParam(value = "客户id数组",required = true)Long[] ids){
        List<Long> idList = Arrays.stream(ids).collect(Collectors.toList());
        try {
            customerService.removeByIds(idList);
        } catch (Exception e) {
            e.printStackTrace();
            throw new AppException(ResponseEnum.FAILED);
        }
        return Result.success(null,"客户删除成功！！");
    }

    @PostMapping("/update")
    @NeedPermissions("customer:update")
    @ApiOperation("修改客户")
    public Result update(@RequestBody CustomerUpdateVO customerUpdateVO) {
        Customer customer = new Customer();
        BeanUtils.copyProperties(customerUpdateVO,customer);
        customerService.updateCustomer(customer);
        return Result.success(null,"客户修改成功！！");
    }
    
}
